#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>

using namespace std;
class Stack {
private:
    int *bottom;
    int *ttop;
    int *_end_of_storage;


public:
    Stack(int capacity = 20): bottom(new int[capacity]), ttop(bottom), _end_of_storage(bottom + capacity){}
    ~Stack(){
        if(bottom){
            delete [] bottom;
            bottom = nullptr;
            ttop = nullptr;
            _end_of_storage = nullptr;
        }
    }
    //判断栈是否为空
    bool empty ()const{
        return ttop == bottom;
    }	
    //判断栈是否已满
    bool full() const{
        return ttop == _end_of_storage;
    }	
    //获取栈总容量
    int capacity()const{
        return _end_of_storage - bottom; 
    }
    //获取当前栈大小
    int size()const{
        return ttop - bottom;
    }

    //扩容
    void recapacity(int newCapacity){
        int *newBottom = new int[newCapacity];

        memcpy(newBottom, bottom, capacity());
        ttop = newBottom + capacity();
        _end_of_storage = newBottom + newCapacity;
        delete [] bottom;
        bottom = newBottom;
        cout<<"------------recapacity-------------"<<endl;
    }
    //元素入栈
    void push(int num){
        if(full()){
            recapacity(capacity() + 20);
        }
        *ttop = num;
        ++ttop;
        cout<<"-------------push------------"<<endl;
    }
    //元素出栈
    void pop(){
        if(empty()){
            cout<<"pop fail, empty!!!"<<endl;
            return;
        }
        *ttop = 0;
        --ttop;
        cout<<"--------------pop------------"<<endl;
    }    
    //获取栈顶元素
    int  top()const{
        if(empty()){
            return -1;
        }
        return *(ttop - 1);
    }	
//...
};

//调试信息
void Print(Stack &s){
    cout<<"size: "<<s.size()<<endl;
    cout<<"capacity: "<<s.capacity()<<endl;
    if(!s.empty())
        cout<<"top num: "<<s.top()<<endl;
    else
        cout<<"-------top fail, empty!!!--------"<<endl;
}

int main()
{
    Stack s(2);
    Print(s);
    s.push(1);
    Print(s);
    s.push(2);
    Print(s);
    s.push(3);
    Print(s);
    s.push(4);
    Print(s);
    s.pop();
    s.pop();
    s.pop();
    s.pop();
    s.pop();
    s.pop();
    Print(s);
    return 0;
}












